package com.gzsxy.demo;


import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * thread是Runnable接口的代理类 而TestThread2类为被代理的对象（），进行统一的操作，
 */
//练习thread，实现多线程同步下载图片
public class TestThread2 implements Runnable {

    private String url;  //网络图片地址
    private String name; //保存的文件名

    public TestThread2(String url, String name) {
        this.url = url;
        this.name = name;
    }

    //下载图片线程的执行体
    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url, name);
        System.out.println("下载了文件名为：" + name);
    }


    public static void main(String[] args) {
        TestThread2 t1 = new TestThread2("https://kuangstudy.oss-cn-beijing.aliyuncs.com/bbs/2021/06/05/kuangstudyb52aafb4-39e7-44b9-9030-59ed75935e23.png", "1.png");
        TestThread2 t2 = new TestThread2("https://kuangstudy.oss-cn-beijing.aliyuncs.com/bbs/2021/06/05/kuangstudyb52aafb4-39e7-44b9-9030-59ed75935e23.png", "2.png");
        TestThread2 t3 = new TestThread2("https://kuangstudy.oss-cn-beijing.aliyuncs.com/bbs/2021/06/05/kuangstudyb52aafb4-39e7-44b9-9030-59ed75935e23.png", "3.png");

        new Thread(t1).start();
        new Thread(t2).start();
        new Thread(t3).start();


        //线程不是立马执行；也是要跟据cpu调度
//        t1.start();
//        t2.start();
//        t3.start();
    }


}




